<?php
/**
 * Project: Vietsam
 * Package Controller
 * Class: Login
 * Author: Hung Nguyen		email: quochungndvt88@yahoo.com
 * Created on Fer 15, 2012
 * Updated on Fer 30, 2012
 *
 * */

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Auth extends MY_Controller {
	

    function __construct() {
    	
        parent::__construct();
        $this->load->model('tab_model', 'tab', true);
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('session');

        $this->template->add_region('new_tours');
        $this->buildNavigation();
        //$this->buildAccountBox();
		//$this->buildSearchByConditionBox();
		//$this->buildSupportBox();
		//$this->buildAdvBox();
		//$this->buildSlider();
		//$this->buildCartBox();
		//$this->buildCompareBox();

    }
    function index() {
    	if($this->checkUserLogin()){
    		redirect("product");	
    	}else{
    		$this->login();
    	}
    }

    
    /*
     * Use template 1 column:
     */
    function new_login(){
    	$data['base_url'] = base_url();
        $this->template->parse_view('full_content1', $this->skins."auth/login_view", $data);
		$this->template->render();
    }
    

	function login(){
		$error_message = '';
        if (isset($_POST['username'])) {

			$user_data = $this->input->post('login', true);
            $email = trim($user_data['username']);
	        $password = trim($user_data['password']);
			//print_r($user_data);

            $email = trim($this->input->post('username', true));
	        $password = (trim($this->input->post('password', true)));
	        
	        $error = false;
	        if ($email == '') {
	            $error = true;
	            $error_message .= $this->lang->line('email_is_required') . '<br>';
	        } else {
	            $this->load->helper('email');
	            if (!valid_email($email)) {
	                $error = true;
	                $error_message .= $this->lang->line('email_is_invalid') . '<br>';
	            }
	        }
	
	        if ($password == '') {
	            $error = true;
	            $error_message .= $this->lang->line('password_is_required') . '<br>';
	        }
	
	        if(!$error) {
                $this->load->model('customer_model', 'customer', true);
				//$user_data = $this->customer->getByEmail($email);
				$user_data = $this->customer->getByEmailPassword($email, $password);
			 //$this->fn_print_r($user_data);
				//if($this->fn_compare_login_password($user_data,$password)){
				if(!empty($user_data)){
                    $this->session->set_userdata('customer', $user_data);
                    
                    redirect('customer');
                    exit();
                } else {
                     $error_message .=  '<div id="error" style="color:red">' . $this->lang->line('invalid_user_name_or_password') . '</div>';
                }
            }
        }
        
        $data['error_message'] = $error_message;
        $data['base_url'] = base_url();
        $this->template->parse_view('full_content1', $this->skins."auth/login_view", $data);
		$this->template->render();
		
	}
	
    function recoveryPassword(){
    	$error_message='';
    	if(isset($_POST['btn_recovery_pass'])){
    		$email = trim($this->input->post('email', true));
	        $error = false;
	        if ($email == '') {
	            $error = true;
	            $error_message .= $this->lang->line('email_is_required') . '<br>';
	        } else {
	            $this->load->helper('email');
	            if (!valid_email($email)) {
	                $error = true;
	                $error_message .= $this->lang->line('email_is_invalid') . '<br>';
	            }
	        }
	        if(!$error) {
                $this->load->model('customer_model', 'customer', true);
				$user_data = $this->customer->getByEmail($email);
				if(!empty($user_data)){
					$new_pass = $this->fn_generate_password();
					$salt = $this->fn_generate_salt();
					$new_pass_hash = $this->fn_generate_salted_password($new_pass, $salt);
					$update['password'] =  $new_pass_hash.":".$salt;
					$update['email'] = $user_data->email;
					$update['firstname'] = $user_data->firstname;
					$update['lastname'] = $user_data->lastname;
					$update['id_user'] = $user_data->id_user;
					if($this->customer->update_account($update)){
						$message='Your username: '.$user_data->email."<br />";
						$message.= 'Your password: ' . $new_pass ."<br />";
						$message .= 'Please click <a href="'.base_url().'auth" target="_blank" >here</a> to login ';
						//$this->send_recovery_pass($user_data->email, $message);
						if($this->send_email($user_data->email, $message)){
							$error_message .= $this->lang->line('recovery_success') . '<br>';
						}
					}
				}else{
					$error_message .= $this->lang->line('account_not_register') . '<br>';
				}
            }
    
    	}
    	$data['error_message'] = $error_message;
        $data['base_url'] = base_url();
        $this->template->parse_view('center_content1', $this->skins."auth/recovery_pass", $data);
		$this->template->render();
    
    }
	function send_recovery_pass($user_email, $message){
		$config = Array(
		  'protocol' => 'smtp',
		  'smtp_host' => 'ssl://smtp.googlemail.com',
		  'smtp_port' => 465,
		  'smtp_user' => 'pmkhoi@gmail.com', // change it to yours
		  'smtp_pass' => 'nhi02329120', // change it to yours
		  'mailtype' => 'html',
		  'charset' => 'iso-8859-1',
		  'wordwrap' => TRUE
		);

		$this->load->library('email', $config);
		$this->email->set_newline("\r\n");
		$this->email->from('pmkhoi@gmail.com'); // change it to yours
		$this->email->to($user_email); // change it to yours
		$this->email->subject('Recovery Password.');
		$this->email->message($message);

		if($this->email->send())
		{
			echo 'Email sent.';
		}
		else
		{
			show_error($this->email->print_debugger());
		}
    	
   }
	function send_email($user_email, $message){
		$this->load->library('email');
    	$this->email->from('hung.nguyen@kns.com.vn', 'Quoc Hung');
		$this->email->to($user_email);
		//$this->email->cc('another@another-example.com');
		//$this->email->bcc('them@their-example.com');
		
		$this->email->subject('Recovery Password');
		$this->email->message($message);
		
		if($this->email->send())
		{
			//echo 'Email sent.';
			$this->email->clear();
			return true;
		}
		else
		{
			//show_error($this->email->print_debugger());
			return false;
		}
    	
   }

    function logout() {
        $this->session->unset_userdata('customer');
        redirect('auth');
    }

}

?>